//
//  SellSortViewController.m
//  LoveCar
//
//  Created by Xue Senhong on 15/5/25.
//  Copyright (c) 2015年 薛森鸿. All rights reserved.
//

#import "SellSortViewController.h"
@interface SellSortViewController ()<UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,SellCellDelegate>

@property (nonatomic, retain)NSDictionary *typeDic;

@property (nonatomic, retain)UIScrollView *typeScroll;

@property (nonatomic, retain)NSMutableArray *buttonArray;

@property (nonatomic, retain)UILabel *typeLabel;

@property (nonatomic, retain)UICollectionView *collect;

@property (nonatomic, retain)NSMutableArray *numArray;

@property (nonatomic, retain)NSMutableArray *listArray;

@property (nonatomic, retain)NSMutableArray *typeNumArray;

@end

@implementation SellSortViewController

- (void)dealloc
{
    [_typeNumArray release];
    [_listArray release];
    [_numArray release];
    [_typeDic release];
    [_typeLabel release];
    [_buttonArray release];
    [_typeScroll release];
    [_collect release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.typeDic = [NSDictionary dictionary];
        self.typeDic = @{@"1":@"小型车", @"2":@"微型车", @"3":@"紧凑型车", @"4":@"中型车", @"5":@"中大型车", @"7":@"SUV", @"8":@"MPV"};
        self.numArray = [NSMutableArray array];
        self.typeNumArray = [NSMutableArray array];
        }
    return self;
}

-(void)loadView
{
    [super loadView];
    [self createCollect];
    [self createScroll];
    
}


#pragma 创建类型的scroll
- (void)createScroll
{
    int num = 0;
    self.typeScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kScreenHeight * 30 / 667)];
    
    self.typeScroll.delegate = self;
    
    [self.view addSubview:self.typeScroll];
    
    self.typeScroll.showsHorizontalScrollIndicator = NO;
    self.buttonArray = [NSMutableArray array];
    for (int i = 0; i < [self.typeDic allKeys].count; i++) {
        NSString *str = [self.typeDic objectForKey:[[self.typeDic allKeys] objectAtIndex:i]];
        [self.typeNumArray addObject:[[self.typeDic allKeys] objectAtIndex:i]];
        UIButton *typeButton = [[UIButton alloc] initWithFrame:CGRectMake(i * kScreenWidth * 15 / 375 + num *  kScreenWidth * 17 / 375, 0, str.length * kScreenWidth * 17 / 375 + kScreenHeight * 15 / 375, kScreenHeight * 30 / 667)];
        
        [typeButton setTitle:str forState:UIControlStateNormal];
        [typeButton release];
        if (i == 0) {
             [typeButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            self.typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kScreenHeight * 25 / 667, kScreenWidth * str.length * 17 / 375 + kScreenWidth * 15 / 375, kScreenHeight * 3 / 667)];
            [self.typeLabel setBackgroundColor:[UIColor blueColor]];
            [typeButton addSubview:self.typeLabel];
            [self.typeLabel release];
        } else {
            [typeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        
        NSValue *valueOfButtonFrame = [NSValue valueWithCGRect:typeButton.frame];
        [self.numArray addObject:valueOfButtonFrame];
        
        [typeButton addTarget:self action:@selector(typeAction:) forControlEvents:UIControlEventTouchUpInside];
        
        
        typeButton.tag = i + 1;
        
        [self.typeScroll addSubview:typeButton];
        num += str.length;
        [self.buttonArray addObject:typeButton];
        [typeButton release];
    }
    self.typeScroll.contentSize = CGSizeMake([self.typeDic allKeys].count * kScreenWidth * 15 / 375 + kScreenWidth * num * 17 / 375, 0);
    [_typeScroll release];
}

#pragma 类型scroll上button的点击事件
- (void)typeAction:(UIButton *)button
{
    __block SellSortViewController *sellSortViewController = self;
    for (int i = 0; i < self.buttonArray.count; i++) {
        if (i == button.tag - 1) {
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        } else {
        [[self.buttonArray objectAtIndex:i] setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    
    self.collect.contentOffset = CGPointMake(kScreenWidth * (button.tag - 1), 0);
    
     [UIView animateWithDuration:0.3 animations:^{
         sellSortViewController.typeLabel.frame = CGRectMake(button.frame.origin.x, kScreenHeight * 22 / 667, button.frame.size.width, 3);
         
     }];
    
    if (button.tag > 3) {
        [UIView animateWithDuration:0.2 animations:^{
            
            sellSortViewController.typeScroll.contentOffset = CGPointMake(kScreenWidth * 121 / 375, 0);
        }];
    } else if (button.tag <= 3) {
        [UIView animateWithDuration:0.2 animations:^{
            
            sellSortViewController.typeScroll.contentOffset = CGPointMake(0, 0);
        }];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 20)];
    titleLabel.textColor = [UIColor blueColor];
    titleLabel.text = @"销量排行";
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];

}

- (void)createCollect
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
    self.collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, kScreenHeight * 30 / 675, self.view.frame.size.width, self.view.frame.size.height - 25 - 64) collectionViewLayout:layout];
    
    self.collect.pagingEnabled = YES; // 整页翻动
    self.collect.bounces = NO; // 到头滑不动
    
    self.collect.delegate = self;
    self.collect.dataSource = self;
    [self.collect registerClass:[SellCollection class] forCellWithReuseIdentifier:@"collect"];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;// 横向滑动
    [self.view addSubview:self.collect];
    [self.collect setBackgroundColor:[UIColor whiteColor]];
    
    [layout release];
    [self.collect release];
}

#pragma collectionView的监听,和上面分类关联
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.collect) {
        int i = self.collect.contentOffset.x / kScreenWidth;
       
        NSValue *rect = [self.numArray objectAtIndex:i];
        CGRect rect1 = [rect CGRectValue];
        
        [UIView animateWithDuration:0.2 animations:^{
            
            self.typeLabel.frame = CGRectMake(rect1.origin.x, kScreenHeight * 25 / 667, rect1.size.width, 3);
        }];
        for (int j = 0; j < [self.typeScroll subviews].count - 1; j++) {
            [[[self.typeScroll subviews] objectAtIndex:j] setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        [[[self.typeScroll subviews] objectAtIndex:i] setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        if (i > 2) {
            [UIView animateWithDuration:0.2 animations:^{
                
                self.typeScroll.contentOffset = CGPointMake(kScreenWidth * 121 / 375, 0);
            }];
        } else if (i <= 2) {
            [UIView animateWithDuration:0.2 animations:^{
                
                self.typeScroll.contentOffset = CGPointMake(0, 0);
            }];
        }
    }
}

#pragma collectionView的协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.typeDic allKeys].count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SellCollection *colletCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collect" forIndexPath:indexPath];
    int Id = [[[self.typeDic allKeys] objectAtIndex:indexPath.row] intValue];
    NSString *string = [NSString stringWithFormat:@"http://api.ycapp.yiche.com/indexdata/GetSerialSaleDataList/?length=20&carlevel=%d&page=1&cityid=0&month=2015-03-01", Id];
    self.listArray = [NSMutableArray array];
    [SANetWorkingTask request:string parmater:nil block:^(NSDictionary *result) {
        NSMutableDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"list"];
        for (NSMutableDictionary *dic in arr) {
            SellSortModel *model = [[[SellSortModel alloc] init]autorelease];
            [model setValuesForKeysWithDictionary:dic];
            [self.listArray addObject:model];
            [model release];
        }
        colletCell.listArray = self.listArray;
        colletCell.delegate = self;
        [[colletCell.subviews objectAtIndex:1] reloadData];
    }];
    return colletCell;

}

#pragma item的Size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kScreenWidth, kScreenHeight);
}


#pragma 协议push
- (void)pushToDetail:(NSIndexPath *)index
{
    SellSortModel *model = [self.listArray objectAtIndex:index.row];
    DetailCarViewController *detail = [[[DetailCarViewController alloc] init] autorelease];
    detail.cellModel.serialId = [NSString stringWithFormat:@"%ld", (long)model.SerialId];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
}


@end
